package com.huang.PC.condition;


import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class zy{
    private  Integer num=1;
    private  Lock lock=new ReentrantLock();
    private  Condition c1 = lock.newCondition();
    private  Condition c2 = lock.newCondition();
    private    Condition c3 = lock.newCondition();

    public void pa(){

        try {
            lock.lock();
            while (num!=1){
                try {
                    c1.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            System.out.println("A"+num);
            num++;
            c2.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();

        }




    }
    public void pb(){
        try {
            lock.lock();
            while (num!=2){

                try {
                    c2.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("B"+num);
            num++;
            c3.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }
    public void pc(){
        try {
            lock.lock();
            while (num!=3){
                try {
                    c3.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            System.out.println("C"+num);
            num=num-2;
            c1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }


    }

}

public class Test {

        //线程操作资源类    定点唤醒 多个condition
    public static void main(String[] args) {
        zy zy = new zy();

        new Thread(()->{
             for (int i = 0; i < 10; i++) {
                    zy.pa();
             }

         }).start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
              zy.pb();

            }

        }).start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
               zy.pc();
            }

        }).start();

    }
}
